/* eslint-disable @typescript-eslint/no-explicit-any */
import myAxios from "@/tool/request";

/**
 * 用户注册
 * @param params
 * @returns
 */
export const userRegisterServer = async (params: any) => {
  try {
    console.log("开始用户注册请求", params);
    const res = await myAxios.request({
      url: "/api/user/register",
      method: "POST",
      data: params,
    });
    console.log("用户注册请求成功", res);
    return res;
  } catch (error) {
    console.error("用户注册请求失败", error);
    throw error; // 重新抛出错误以便调用方处理
  }
};

/**
 * 用户登录
 * @param params
 * @returns
 */
export const userLoginServer = async (params: any) => {
  try {
    console.log("开始用户登录请求", params);
    const res = await myAxios.request({
      url: "/api/user/login",
      method: "POST",
      data: params,
    });
    console.log("用户登录请求成功", res);
    return res;
  } catch (error) {
    console.error("用户登录请求失败", error);
    throw error;
  }
};

/**
 * 用户退出
 * @param params
 * @returns
 */
export const userLogoutServer = async (params: any) => {
  try {
    const res = await myAxios.request({
      url: "/api/user/logout",
      method: "POST",
      data: params,
    });
    console.log("用户退出请求成功", res);
    return res;
  } catch (error) {
    console.error("用户退出请求失败", error);
    throw error;
  }
};

/**
 * 获取当前用户信息
 * @param params
 * @returns
 */
export const getCurrentUserServer = async () => {
  try {
    const res = await myAxios.request({
      url: "/api/user/current",
      method: "GET",
    });
    console.log("获取当前用户信息成功", res);
    return res;
  } catch (error) {
    console.error("获取当前用户信息失败", error);
    throw error;
  }
};

/**
 * 搜索用户
 * @param params
 * @returns
 */
export const searchUserServer = async (username: any) => {
  try {
    const res = await myAxios.request({
      url: "/api/user/search",
      method: "GET",
      params: { username },
    });
    console.log("搜索用户成功", res);
    return res;
  } catch (error) {
    console.error("搜索用户失败", error);
    throw error;
  }
};

/**
 * 删除用户
 * @param params
 * @returns
 */
export const deleteUserServer = async (id: string) => {
  try {
    const res = await myAxios.request({
      url: "/api/user/delete",
      method: "POST",
      data: id,
      // 关键点：要传递 JSON 格式的值
      headers: {
        "Content-Type": "application/json",
      },
    });
    console.log("删除用户成功", res);
    return res;
  } catch (error) {
    console.error("删除用户失败", error);
    throw error;
  }
};
